<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

/**
 * 动态
 *
 */
class Active extends CI_Controller {
	function __construct()
	{
		parent::__construct();
		//加载usermodel
		$this->load->model('usermodel');
		$this->uid = $this->usermodel->get_id_by_token($this->input->post('token'));
		$this->load->model('activemodel');
	}
	
	function index()
	{
		$this->myactives();
	}
	
	/**
	 * 我的动态
	 *
	 */
	function myactives()
	{
		header('Content-type:text/xml');// 向浏览器输出 xml
		$actives = $this->_get_actives($this->uid);
		if(count($actives) > 0){
			//查询动态总数
			$this->db->select('count(*) count')
					 ->from('active')
					 ->where('uid', $this->uid);
			$query = $this->db->get();
			$row = $query->row_array();
			$active_count = $row['count'];
			
			//输出最终的xml
			$this->xmllib->append_xml('active_count', $active_count);
			echo $this->xmllib->get_xml($actives, array('active'), 'actives');
			exit;
		}else{
			//结果集为空
			echo $this->xmllib->empty_xml();
			exit;
		}
	}
	
	/**
	 * 查看别人动态
	 *
	 */
	function other_myactives()
	{
		header('Content-type:text/xml');// 向浏览器输出 xml
		$uid = $this->input->post('uid');
		$actives = $this->_get_actives($uid);
		if(count($actives) > 0){
			//查询动态总数
			$this->db->select('count(*) count')
					 ->from('active')
					 ->where('uid', $uid);
			$query = $this->db->get();
			$row = $query->row_array();
			$active_count = $row['count'];
			
			//输出最终的xml
			$this->xmllib->append_xml('active_count', $active_count);
			echo $this->xmllib->get_xml($actives, array('active'), 'actives');
			exit;
		}else{
			//结果集为空
			echo $this->xmllib->empty_xml();
			exit;
		}
	}
	
	/**
	 * 评论动态
	 *
	 */
	function commentactive()
	{
		header('Content-type:text/xml');// 向浏览器输出 xml
		//加载表单验证类库
  		$this->load->library('form_validation');
		$_POST['activeid'] = isset($_POST['activeid']) ? $_POST['activeid'] : '';//此处设置时为了防止生成xml报错
		$this->form_validation->set_rules('activeid', '要点评的动态', 'required');
		$this->form_validation->set_rules('content', '点评内容', 'trim|required|max_length[140]|xss_clean');
		$this->form_validation->set_message('required', '%s不能为空');
		$this->form_validation->set_message('max_length', '%s字数不要超过 %d');
		$this->form_validation->set_error_delimiters('', '');
		if ($this->form_validation->run() == FALSE){
			//错误提示
			$activeid_error = form_error('activeid');
			$content_error = form_error('content');
			if($activeid_error)
				echo $this->xmllib->error_xml($activeid_error);
			elseif ($content_error)
				echo $this->xmllib->error_xml($content_error);
			exit;
		}else{
			//验证通过
			$activeid = $this->input->post('activeid');
			$content = $this->input->post('content');
			//查询要评论的应用是否存在
			$this->load->model('activemodel');
			if(!$this->activemodel->check_active($activeid)){
				//此动态不存在
				echo $this->xmllib->error_xml('您要评论的动态不存在');
				exit;
			}
			//添加评论信息
			$this->load->model('commentmodel');
			$data = array('comment_id' 		=> $activeid, 
						  'comment_type'	=> ACTIVE_COMMENT,
						  'uid' 			=> $this->uid, 
						  'content' 		=> $content,
						  'share_or_comment'=> IS_COMMENT);
			if($this->commentmodel->insert_comment($data)){
				//需要添加动态
				$this->load->model('activemodel');
				$this->activemodel->insert_active(array('uid' 			=> $this->uid,
														'active_id'		=> $activeid,
														'active_type'	=> ACTIVE_ACTIVE,
														'memo'			=> '评论了'));
				echo $this->xmllib->success_xml('评论添加成功');
				exit;
			}else{
				echo $this->xmllib->error_xml('评论失败，请稍候再试');
				exit;
			}
		}
	}
	
	//获取个人动态
	function _get_actives($uid='')
	{
		//获取动态信息
		$this->dblib->set_query_limit();
		$this->db->select('u.name as user_name, u.photo as user_photo, u.reg_type, a.createtime as active_createtime, (UNIX_TIMESTAMP(NOW())-UNIX_TIMESTAMP(a.createtime)) as diff_time, a.active_id, a.active_type, a.memo as active_memo')
				 ->from('active as a')
				 ->join('user as u', 'u.id=a.uid')
				 ->where('u.id', $uid)
				 ->order_by('active_createtime', 'desc');
		$query = $this->db->get();
		$actives = $query->result_array();
		foreach ($actives as $k=>&$active){
			//设置图片路径
			$this->load->library('uploadimglib');
			$actives[$k]['user_photo'] = $this->uploadimglib->re_user_photo($active['user_photo'], $active['reg_type']);
			unset($actives[$k]['reg_type']);//不需要此字段
			$actives[$k]['diff_time'] = $this->_diff_time($active['diff_time']);
			switch ($active['active_type']){
				case APP_ACTIVE:
					//应用
					$this->db->select('a.id, a.name, a.photo, c.share_or_comment, c.content')
							 ->from('app as a')
							 ->join('comments as c', "a.id=c.comment_id AND c.uid='$uid' AND c.comment_type=" . APP_COMMENT, 'left')
							 ->where('a.id', $active['active_id']);
					$query = $this->db->get();
					$row = $query->row_array();
					$actives[$k] = array_merge($active, $row);
					$actives[$k]['photo'] = $this->uploadimglib->re_image_path($row['photo']);
					break;
				case ALBUM_ACTIVE:
					//专辑
					$this->db->select('a.id, a.name, c.share_or_comment, c.content')
							 ->from('album as a')
							 ->join('comments as c', "a.id=c.comment_id AND c.uid='$uid' AND c.comment_type=" . ALBUM_COMMENT, 'left')
							 ->where('a.id', $active['active_id']);
					$query = $this->db->get();
					$row = $query->row_array();
					$actives[$k] = array_merge($active, $row);
					break;
				case ATTENTION_ACTIVE:
					//关注
					$this->db->select('id, name, photo')
							 ->from('user')
							 ->where('id', $active['active_id']);
					$query = $this->db->get();
					$row = $query->row_array();
					$actives[$k] = array_merge($active, $row);
					$actives[$k]['photo'] = $this->uploadimglib->re_image_path($row['photo']);
					break;
				case ACTIVE_ACTIVE:
					//动态本身
					/*$this->db->select('a.id, a.name, c.share_or_comment, c.content')
							 ->from('active as a')
							 ->join('comments as c', "a.id=c.comment_id AND c.uid='$uid' AND c.comment_type=" . ACTIVE_COMMENT, 'left')
							 ->where('a.id', $active['active_id']);
					$query = $this->db->get();
					$row = $query->row_array();
					$actives[$k] = array_merge($active, $row);*/
					break;
			}
		}
		
		return $actives;
	}
	
	//时间间隔
	function _diff_time($time)
	{
		//加载timelib
		$this->load->library('timelib');
		return $this->timelib->diff_time($time);
	}
}

/* End of file active.php */
/* Location: ./application/controllers/active.php */